package cst.a722wr.zyfypt_722wr.Activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.ActionMenuItemView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import cst.a722wr.zyfypt_722wr.R;
import cst.a722wr.zyfypt_722wr.iface.CollectListener;
import cst.a722wr.zyfypt_722wr.iface.FocusListener;
import cst.a722wr.zyfypt_722wr.model.CollectModel;
import cst.a722wr.zyfypt_722wr.model.FocusModel;

public class ProjectActivity extends AppCompatActivity {

    private String BASEURL="http://amicool.neusoft.edu.cn/";
    private WebView webView;
    private int resid;//资源id
    private int userid;//资源用户id
    private Boolean flagcollect = false;
    private Boolean flagfocus = false;
    private CollectModel collectmodel;
    private FocusModel focusModel;
    private SharedPreferences sp;
    private String sessionID = "";

    CollectListener collectListener = new CollectListener() {
        @Override
        public void onResponse(String msg) {
            ActionMenuItemView item = (ActionMenuItemView)findViewById(R.id.collect);
            switch (msg)
            {
                case "2": System.out.println("----收藏成功");
                    flagcollect=true;
                    item.setTitle("取消收藏");
                    break;
                case "1":System.out.println("----收藏失败");
                    break;
                case "5":System.out.println("----取消收藏成功");
                    flagcollect=false;
                    item.setTitle("收藏");
                    break;
                case "4":System.out.println("----取消收藏失败");
                    break;
                case "7":System.out.println("----已收藏");
                    flagcollect=true;
                    item.setTitle("取消收藏");
                    break;
                case "8":System.out.println("----未收藏");
                    flagcollect=false;
                    item.setTitle("收藏");
                    break;
                default:
                    Toast.makeText(ProjectActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(ProjectActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    };

    FocusListener focusListener = new FocusListener() {
        @Override
        public void onResponse(String msg) {
            ActionMenuItemView item = (ActionMenuItemView)findViewById(R.id.focus);
            switch (msg)
            {
                case "2": System.out.println("----关注成功");
                    flagfocus=true;
                    item.setTitle("取消关注");
                    break;
                case "1": System.out.println("----关注失败");
                    break;
                case "5": System.out.println("----取消关注成功");
                    flagfocus=false;
                    item.setTitle("关注");
                    break;
                case "4": System.out.println("----取消关注失败");
                    break;
                case "7": System.out.println("----已关注");
                    flagfocus=true;
                    item.setTitle("取消关注");
                    break;
                case "8": System.out.println("----未关注");
                    flagfocus=false;
                    item.setTitle("关注");
                    break;
                default:
                    Toast.makeText(ProjectActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(ProjectActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project);
        Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        resid = getIntent().getIntExtra("resid", 1);
        userid = getIntent().getIntExtra("userid", 7);
        sp = getSharedPreferences("ME", MODE_PRIVATE);
        sessionID = sp.getString("session_id", null);

        webView = (WebView) findViewById(R.id.project_webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl(BASEURL+"project.php/show/index/id/"+resid);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        collectmodel = new CollectModel();
        collectmodel.exist("project", resid, sessionID, collectListener);
        focusModel = new FocusModel();
        focusModel.exist("userfocus", userid, sessionID, focusListener);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.collect:
                //Toast.makeText(this, "文章收藏", Toast.LENGTH_SHORT).show();
                if(flagcollect)//如果已收藏，则调用取消收藏
                {
                    System.out.println("----准备取消收藏");
                    collectmodel.uncollect("project",resid,sessionID,collectListener);
                }
                else//如果未收藏，则调用收藏
                {
                    System.out.println("----准备收藏");
                    collectmodel.collect("project",resid,sessionID,collectListener);
                }
                break;
            case R.id.focus:
                //Toast.makeText(this, "文章关注", Toast.LENGTH_SHORT).show();
                if(flagfocus)//如果已关注，则调用取消关注
                {
                    System.out.println("----准备取消关注");
                    focusModel.unfocus("userfocus", userid, sessionID, focusListener);
                }
                else//如果未关注，则调用关注
                {
                    System.out.println("----准备关注");
                    focusModel.focus("userfocus", userid, sessionID, focusListener);
                }
                break;
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
